package com.dp.singleton;

/**
 * @Author: Ruomeng
 * @Date: 2024/11/6
 * @Description : 解决序列化对单例模式的破坏
 * @Version: 1.0
 */
public class SerializableSingleton {

    public static volatile SerializableSingleton instance;

    private SerializableSingleton() {
    }

    public static SerializableSingleton getInstance() {
        if (instance == null) {
            synchronized (SerializableSingleton.class) {
                if (instance == null) {
                    instance = new SerializableSingleton();
                }
            }
        }
        return instance;
    }

    // 反序列化时返回Singleton的instance
    private Object readResolve() {
        return getInstance();
    }

}
